package cstxch616.recycleproject.acitivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import cstxch616.recycleproject.R;
import cstxch616.recycleproject.bean.UserBean;
import cstxch616.recycleproject.iface.LoginListener;
import cstxch616.recycleproject.model.LoginModel;

public class loginActivity extends AppCompatActivity implements LoginListener,View.OnClickListener{
    private Button btn2;
    private Button btn1;
    private EditText et_userName, et_password;
    private CheckBox checkBox_remember;
    private Context context;
    private SharedPreferences preferences;
    final static String PREF_NAME="loginPref";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        context =loginActivity.this;
        init();
        preferences= getSharedPreferences(PREF_NAME,MODE_PRIVATE);
        boolean isChecked =preferences.getBoolean("isChecked",false);
        if(isChecked)
        {
            String userName=preferences.getString("userName","");
            String password =preferences.getString("password","");
            et_userName.setText(userName);
            et_password.setText(password);
            checkBox_remember.setChecked(isChecked);
        }

    }
    private void init() {
        btn2=(Button)findViewById(R.id.button2);
        btn1=(Button)findViewById(R.id.button);
        et_userName=(EditText)findViewById(R.id.editText2) ;
        et_password=(EditText)findViewById(R.id.editText3) ;
        btn2.setOnClickListener(this);
        btn1.setOnClickListener(this);
        checkBox_remember=(CheckBox)findViewById(R.id.checkBox);
    }
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button2:
                Intent intent=new Intent(loginActivity.this,ZcActivity.class);
                startActivity(intent);
                break;
            case R.id.button:
                String username=et_userName.getText().toString();
                String password=et_password.getText().toString();
                LoginModel mode=new LoginModel();
                mode.login(username,password,this);
                break;
        }
    }

    @Override
    public void onResponse(UserBean userBean) {
        String sessionId=userBean.getSessionid();
        SharedPreferences.Editor editor =preferences.edit();
        editor.putString("sessionId",sessionId);
        editor.putBoolean("isChecked",checkBox_remember.isChecked());
        if(checkBox_remember.isChecked()){
            editor.putString("userName",et_userName.getText().toString());
            editor.putString("password",et_password.getText().toString());

        }
        editor.commit();
        Intent intent=new Intent(context,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onFail(String msg) {

    }
}
